//
//  oldSearchViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/23.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldSearchViewController.h"
#import "oldSearchTitleView.h"
#import "oldSearchTableViewCell.h"
#import "oldServiceDetailInfoViewController.h"

@interface oldSearchViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)oldSearchTitleView *searchView;
@property (nonatomic, copy)NSArray *dataSource;
@end

static NSString *const identifier = @"oldSearchTableViewCell";

@implementation oldSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    [self rigisterCell:identifier];
    [self initTitleView];
}

- (void)initTitleView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    
    _searchView = [[[NSBundle mainBundle] loadNibNamed:@"oldSearchTitleView"
                                                owner:self
                                              options:nil] objectAtIndex:0];
    _searchView.frame = view.bounds;
    _searchView.text_filed.delegate = self;
    [view addSubview:_searchView];
    
    self.navigationItem.titleView = view;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_searchView.text_filed becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [_searchView.text_filed resignFirstResponder];
}

#pragma mark - textfiled
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [_searchView.text_filed resignFirstResponder];
    [self search];
    return YES;
}

- (void)search
{
    if (![_searchView.text_filed.text length]) {
        return;
    }
    
    WeakSelf
    
    [oldHub waiting:@"搜索中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_SearchServeCategory params:@{@"keyword":_searchView.text_filed.text} success:^(id data) {
        [oldHub hideHUD];
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < [data count]; i++) {
        oldService *service = [[oldService alloc] initWithDictionary:data[i]];
        [array addObject:service];
    }
    
    _dataSource = array;
}

#pragma mark - initData
- (void)initData
{
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldSearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.service = _dataSource[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldServiceDetailInfoViewController *vc = [oldServiceDetailInfoViewController new];
    vc.service = _dataSource[indexPath.row];
    [self.navigationController popToRootViewControllerAnimated:NO];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
